<?php

namespace common\model;

use think\model\RelationModel;

class PlanModel extends RelationModel
{

    //自动验证
    protected $_validate = [
        ['project_id', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT], // 必须字段
        ['episode_id', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT], // 必须字段
        ['step_category_ids', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT], // 必须字段
        ['project_id,episode_id,step_category_name', '', '', self::EXISTS_VALIDATE, 'unique'],
    ];

    //自动完成
    protected $_auto = [
        ['tenant_id', 'auto_fill_tenant_id', self::MODEL_INSERT, 'function'],
        ['created_by', 'fill_created_by', self::MODEL_INSERT, 'function'],
        ['updated_by', 'fill_created_by', self::MODEL_UPDATE, 'function'],
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['updated', 'time', self::MODEL_UPDATE, 'function'],
        ['step_category_ids', 'json_encode', self::EXISTS_VALIDATE, 'function'],
    ];

    /**
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getStepCategoryIdsAttr($value, $data)
    {
        if (empty($value)) {
            return [];
        }
        return json_decode($value, true);
    }
}
